home *** CD-ROM | disk | FTP | other *** search
/ Pascal Super Library / Pascal Super Library (CW International)(1997).bin / SORT_UTL / MRGSORT / CLOCK.ASM < prev    next >
Assembly Source File  |  1990-02-17  |  3KB  |  89 lines

  1. biosdat    equ    040h;        segment for bios data
  2. ticker    equ    06ch;        offset of tick counter, 3 bytes
  3. ; we also reference the overflow byte at 070h implicitly
  4.  
  5. ; We cannot define a 'segment at' because the TP linkage barfs
  6.  
  7. code    segment word
  8. public    initc
  9. public    clk
  10.  
  11. assume    cs : code
  12. ;
  13. ; Reprogram the timer to operate in mode 2.  This will cause
  14. ; the interrupt to be a 1 uS pulse, and MAY cause some machines
  15. ; to ignore the interrupt.  Each invocation will probably lose 
  16. ; about 10 uSecs of system time, depending on CPU clock rate.
  17. ; This should be negligible compared to actual clock accuracy.
  18. initc    proc    near
  19.     mov    ax,biosdat
  20.     mov    es,ax
  21.     mov    di,ticker
  22.     mov    al,es:[di]
  23. initc1:    cmp    al,es:[di];    To avoid fouling the time of day
  24.     jz    initc1;        wait for a clock tick, synchronize
  25.     cli;            If interrupts weren't on we never got here
  26.     mov    al,034h;    timer 0, mode 2, 2 byte rd/wrt, binary
  27.     out    043h,al;    reprogram it.  This will lose a few uS
  28.     xor    al,al
  29.     out    040h,al;    set the register 0
  30.     out    040h,al;    MUST write both
  31.     sti;            re-arm interrupts
  32.     ret
  33. initc    endp
  34.  
  35. ; FUNCTION clock : real;     (* The time unit is hours *)
  36. ; The value returned is 128 larger than 'hours after midnight'.  By
  37. ; not making any correction here we avoid doing any floating point
  38. ; arithmetic, and make capturing the clock a fast operation.  The
  39. ; fundamental 'hours' unit reflects the actual clock operation, so
  40. ; that accuracy is dependant only on the timebase oscillator.
  41. ;
  42. ; This code is highly dependant on the 6 byte TP real format.  It 
  43. ; will not mix with programs that have redeclared a real to be one
  44. ; of the 8087 formats.  The clock resolution is about 0.85 uS.
  45. ;
  46. ; Since the clock overflow bit is set at midnight, and reset by a 
  47. ; bios time enquiry, performing any other time accesses can cause
  48. ; erroneous results for times straddling midnight.  This can be 
  49. ; detected by the fact that the clock value became smaller, and
  50. ; the later value should be increased by 24.  After two occurances
  51. ; of midnight all bets are off.
  52. clk    proc    near
  53.     push    ds
  54.     mov    ax,biosdat
  55.     mov    ds,ax
  56.     mov    di,ticker;    monitor occurence of timer ticks.
  57. clk1:    mov    si,di;        which can cause peculiar results
  58.     mov    ch,[di]
  59.     mov    al,0
  60.     pushf
  61.     cli
  62.     out    043h,al;    access timer 0
  63.     in    al,040h;    get lsb
  64.     mov    ah,al
  65.     in    al,040h;    get msb
  66.     popf;            restore interrupt enable setting
  67.     not    ax;        timer counts down.
  68.     mov    bl,al
  69.     lodsb
  70.     mov    bh,al
  71.     lodsb
  72.     mov    dl,al
  73.     lodsb
  74.     mov    dh,al
  75.     inc    si
  76.     inc    si;        point to timer overflow
  77.     test    byte ptr [si],0ffh
  78.     jz    clk2;        no overflow
  79.     add    dh,24;        else add 24 hours
  80. clk2:    cmp    ch,[di];    check no interrupt snuck in
  81.     jnz    clk1;        if so repeat, get consistent values
  82.     mov    al,088h;    set binary point
  83.     pop    ds
  84.     ret
  85. clk    endp
  86.  
  87. code    ends
  88.     end
  89. x╕